package com.koron.sinoprobe.web.plan.service.impl;

import com.koron.sinoprobe.config.Constants;
import com.koron.sinoprobe.web.ledger.bean.entity.SpProject;
import com.koron.sinoprobe.web.ledger.bean.query.SpPlanMilestoneQuery;
import com.koron.sinoprobe.web.ledger.bean.vo.SpProjectVO;
import com.koron.sinoprobe.web.ledger.service.SpProjectService;
import com.koron.sinoprobe.web.plan.bean.entity.SpPlanMilestone;
import com.koron.sinoprobe.web.plan.bean.vo.SpPlanMilestoneVO;
import com.koron.sinoprobe.web.plan.mapper.SpPlanMilestoneMapper;
import com.koron.sinoprobe.web.plan.service.SpPlanMilestoneService;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;

/**
 * @author twcao
 * @title: SpPlanMilestoneServiceImpl
 * @projectName GDH_EAM_V2
 * @description: 里程碑service实现
 * @date 2021/12/28:45
 */
@Service
public class SpPlanMilestoneServiceImpl implements SpPlanMilestoneService {

    @Autowired
    private SpProjectService projectService;

    /**
     * @param factory
     * @param bean
     * @return
     */
    @Override
    @TaskAnnotation("updateById")
    public SpPlanMilestone updateById(SessionFactory factory, SpPlanMilestone bean) {
        SpPlanMilestoneMapper mapper = factory.getMapper(SpPlanMilestoneMapper.class);
        Assert.notNull(bean, "里程碑不能为空");
        Assert.hasText(bean.getId(), "里程碑id不能为空");
        SpPlanMilestoneVO db = mapper.getById(bean.getId());
        Assert.isTrue(!StringUtils.equals(db.getStatus(), Constants.MilestoneStatus.FINISH), "已完成的里程碑禁止修改");
        Assert.hasText(bean.getSpId(), "项目id不能为空");
        if (bean.getFactEndTime() != null && bean.getFactStartTime() != null) {
            Assert.isTrue(bean.getFactEndTime().compareTo(bean.getFactStartTime()) >= 0, "结束时间不得早于开始时间");
        }
        SpProjectVO spProjectVO = projectService.selectOne(factory, bean.getSpId());
        spProjectVO.setCurrMilestone(bean.getName());
        spProjectVO.setMilestoneStatus(getSpMilestoneStatus(factory, bean));
        spProjectVO.setUpdateFlag(bean.getUpdateFlag());
        projectService.saveOrUpdate(factory, (SpProject) spProjectVO);
        if ("NOT_START".equals(bean.getStatus())) {
            mapper.updateNotStart(bean);
            return bean;
        }
        mapper.updateById(bean);
        return bean;
    }

    @Override
    @TaskAnnotation("getBySpId")
    public List<SpPlanMilestoneVO> getBySpId(SessionFactory factory, String spId) {
        SpPlanMilestoneMapper mapper = factory.getMapper(SpPlanMilestoneMapper.class);
        Assert.hasText(spId, "项目或计划id不能为空");
        return mapper.getBySpId(spId);
    }

    @Override
    @TaskAnnotation("queryList")
    public List<SpPlanMilestoneVO> queryList(SessionFactory factory, SpPlanMilestoneQuery query) {
        SpPlanMilestoneMapper mapper = factory.getMapper(SpPlanMilestoneMapper.class);
        return mapper.queryList(query);
    }

    @Override
    @TaskAnnotation("queryNameList")
    public List<String> queryNameList(SessionFactory factory, String name) {
        SpPlanMilestoneMapper mapper = factory.getMapper(SpPlanMilestoneMapper.class);
        return mapper.queryNameList(name);
    }

    public String getSpMilestoneStatus(SessionFactory sessionFactory, SpPlanMilestone bean) {
        SpPlanMilestoneMapper mapper = sessionFactory.getMapper(SpPlanMilestoneMapper.class);
        String spId = bean.getSpId();

        // 如果当前里程碑列表信息所有实际结束时间都填写完成则状态为完成
        String lastMilestoneId = mapper.findLastMilestone(spId);
        if (lastMilestoneId.equals(bean.getId()) && bean.getFactEndTime() != null) {
            return Constants.MilestoneStatus.FINISH;
        } else {
            // 计划结束时间
            Date expectEndTime = bean.getExpectEndTime();
            // 实际结束时间
            Date factEndTime = bean.getFactEndTime();
            if (factEndTime != null && factEndTime.compareTo(expectEndTime) > 0) {
                return Constants.MilestoneStatus.LAG;
            } else {
                return Constants.MilestoneStatus.NORMAL;
            }
        }
    }

}
